package com.example.lee.democ;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button btnQuery;
    private TextView result;
    private TextView delStuno;
    private Button btnDel;
    private TextView edtStuno;
    private TextView edtEnglish;
    private TextView edtAndroid;
    private Button btnAdd;

    private TextView changeStuno;
    private TextView changeEnglish;
    private TextView changeAndroid;
    private Button changeBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        result = findViewById(R.id.result);

        /**
         * 查询学生表
         */
        btnQuery = findViewById(R.id.btnQuery);
        btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://lee.student/score");
                Cursor cursor = getContentResolver().query(uri,null,null,null,null);

                StringBuffer buffer = new StringBuffer();
                if (cursor != null && cursor.getCount() > 0) {
                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        String stuno = cursor.getString(0);
                        int english = cursor.getInt(1);
                        int Android = cursor.getInt(2);

                        buffer.append(stuno).append("  ").append(english).append("  ").append(Android).append("\n");
                    }
                    result.setText(buffer);
                }else {
                    result.setText("无数据");
                }
            }
        });


        delStuno = findViewById(R.id.delStuno);
        btnDel = findViewById(R.id.btnDel);
        btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://lee.student/score");
                String no = delStuno.getText().toString();
                String where = "stuno = " + no;
                int p = getContentResolver().delete(uri,where,null);
                Toast.makeText(MainActivity.this,"删除了" + p + "行数据",Toast.LENGTH_SHORT).show();
            }
        });

        edtStuno = findViewById(R.id.edtStuno);
        edtEnglish = findViewById(R.id.edtEnglish);
        edtAndroid = findViewById(R.id.edtAndroid);
        btnAdd = findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String no = edtStuno.getText().toString();
                String english = edtEnglish.getText().toString();
                String android = edtAndroid.getText().toString();


                Uri uri = Uri.parse("content://lee.student/score");
                ContentValues contentValues = new ContentValues();
                contentValues.put("stuno",no);
                contentValues.put("english",english);
                contentValues.put("Android",android);
                getContentResolver().insert(uri,contentValues);
            }
        });

        changeStuno = findViewById(R.id.changeStuno);
        changeEnglish = findViewById(R.id.changeEnglish);
        changeAndroid = findViewById(R.id.changeAndroid);
        changeBtn = findViewById(R.id.changeBtn);
        changeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String no = changeStuno.getText().toString();
                String english = changeEnglish.getText().toString();
                String android = changeAndroid.getText().toString();



                Uri uri = Uri.parse("content://lee.student/score");
                String where = "stuno = " + no;

                ContentValues value = new ContentValues();
                value.put("english",english);
                value.put("Android",android);

                int i = getContentResolver().update(uri,value,where,null);
                Toast.makeText(MainActivity.this,"更新了" + i + "行数据",Toast.LENGTH_SHORT).show();
            }
        });

    }
}
